package com.zjitc.common.redis;

import com.zjitc.common.Gsonal;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/4/10 15:53
 * @description:
 */
public class MyRedisPool implements RedisCache {
    //Jredis
    @Autowired
    private JedisPool pool;


    //=============================key============================

    @Override
    public boolean expire(String key, int time) {
        Jedis jedis = pool.getResource();
        try {
            if (time > 0) {
                jedis.expire(key, time);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            jedis.close();
        }
    }

    @Override
    public long getExpire(String key) {
        Jedis jedis = pool.getResource();
        try {
            return jedis.ttl(key);
        } finally {
            jedis.close();
        }
    }

    @Override
    public long moveExpire(String key) {
        Jedis jedis = pool.getResource();
        try {
            return jedis.persist(key);
        } finally {
            jedis.close();
        }
    }

    @Override
    public boolean exists(String key) {
        Jedis jedis = pool.getResource();
        try {
            return jedis.exists(key);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            jedis.close();
        }
    }

    @Override
    @SuppressWarnings("unchecked")
    public long del(String... keys) {
        Jedis jedis = pool.getResource();
        long count = 0;
        try {
            for (int i = 0; i < keys.length; i++) {
                Long del = jedis.del(keys[i]);
                count += del;
            }
            return count;
        } finally {
            jedis.close();
        }
    }

    //============================String=============================
    @Override
    public Object get(String key) {
        Jedis jedis = pool.getResource();
        try {
            return key == null ? null : jedis.get(key);
        } finally {
            jedis.close();
        }
    }

    @Override
    public boolean set(String key, String value) {
        Jedis jedis = pool.getResource();
        try {
            jedis.set(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            jedis.close();
        }

    }

    @Override
    public boolean set(String key, String value, int time) {
        Jedis jedis = pool.getResource();
        try {
            if (time > 0) {
                jedis.set(key, value);
                expire(key, time);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            jedis.close();
        }
    }

    @Override
    public long increase(String key, long delta) {
        Jedis jedis = pool.getResource();
        if (delta < 0) {
            throw new RuntimeException("递增因子必须大于0");
        }
        try {
            return jedis.incrBy(key, delta);
        } finally {
            jedis.close();
        }
    }

    @Override
    public long decrease(String key, long delta) {
        Jedis jedis = pool.getResource();
        if (delta < 0) {
            throw new RuntimeException("递减因子必须大于0");
        }
        try {
            return jedis.decrBy(key, delta);
        } finally {
            jedis.close();
        }
    }

    //================================Hash=================================

    @Override
    public boolean existsHash(String key, String field) {
        Jedis jedis = pool.getResource();
        try {
            return jedis.hexists(key, field);
        } finally {
            jedis.close();
        }
    }

    @Override
    public String hashMapget(String key, String field) {
        Jedis jedis = pool.getResource();
        try {
            return jedis.hget(key, field);
        } finally {
            jedis.close();
        }
    }

    @Override
    public Map<String, String> hashMapGetAll(String key) {
        Jedis jedis = pool.getResource();
        try {
            return jedis.hgetAll(key);
        } finally {
            jedis.close();
        }
    }


    @Override
    public <T> List<T> hashListGetAll(String keys, Class<T> classOfT) {
        Jedis jedis = pool.getResource();
        try {
            Map<String, String> mapGetAll = hashMapGetAll(keys);
            List<T> list = new ArrayList<>();
            mapGetAll.forEach((key, value) -> {
                T t = Gsonal.fromJson(value, classOfT);
                list.add(t);
            });
            return list;
        } finally {
            jedis.close();
        }
    }

    @Override
    public <T> T hashgetObject(String key, String filed, Class<T> classOfT) {
        Jedis jedis = pool.getResource();
        try {
            String json = hashMapget(key, filed);
            T t = Gsonal.fromJson(json, classOfT);
            return t;
        } finally {
            jedis.close();
        }
    }

    @Override
    public Long hashset(String key, String filed, String value) {
        Jedis jedis = pool.getResource();
        try {
            return jedis.hset(key, filed, value);
        } finally {
            jedis.close();
        }
    }

    @Override
    public String hashMapset(String key, Map<String, String> map) {
        Jedis jedis = pool.getResource();
        try {
            return jedis.hmset(key, map);
        } catch (Exception e) {
            e.printStackTrace();
            return "存储中出错";
        } finally {
            jedis.close();
        }
    }

    @Override
    public String hashMapset(String key, Map<String, String> map, int time) {
        Jedis jedis = pool.getResource();
        try {
            String hmset = jedis.hmset(key, map);
            expire(key, time);
            return hmset;
        } catch (Exception e) {
            e.printStackTrace();
            return "存储中出错";
        } finally {
            jedis.close();
        }
    }

    @Override
    public Long hashExistset(String key, String field, String value) {
        Jedis jedis = pool.getResource();
        try {
            Long count = jedis.hsetnx(key, field, value);
            return count;
        } finally {
            jedis.close();
        }
    }

    @Override
    public Long hashExistset(String key, String field, String value, int time) {
        Jedis jedis = pool.getResource();
        try {
            Long aLong = jedis.hsetnx(key, field, value);
            if (time > 0) {
                expire(key, time);
            }
            return aLong;
        } finally {
            jedis.close();
        }
    }

    @Override
    public List<String> hashkeys(String key) {
        Jedis jedis = pool.getResource();
        try {
            Set<String> hkeys = jedis.hkeys(key);
            List<String> files = new ArrayList<>(hkeys);
            return files;
        } finally {
            jedis.close();
        }
    }

    @Override
    public void delHash(String key, String... field) {
        Jedis jedis = pool.getResource();
        try {
            jedis.hdel(key, field);
        } finally {
            jedis.close();
        }
    }

    @Override
    public void hincrby(String key, String field, Integer increment) {
        Jedis jedis = pool.getResource();
        try{
            jedis.hincrBy(key,field,increment);
        }finally {
            jedis.close();;
        }
    }

    //============================set=============================
    @Override
    public Set<String> getAllSet(String key) {
        Jedis jedis = pool.getResource();
        try {
            return jedis.smembers(key);
        } finally {
            jedis.close();
        }
    }

    @Override
    public boolean existsSet(String key, String value) {
        Jedis jedis = pool.getResource();
        try {
            return jedis.sismember(key, value);
        } finally {
            jedis.close();
        }
    }

    @Override
    public long addSet(String key, String... values) {
        Jedis jedis = pool.getResource();
        try {
            return jedis.sadd(key, values);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        } finally {
            jedis.close();
        }
    }

    @Override
    public long addSet(String key, int time, String... values) {
        Jedis jedis = pool.getResource();
        try {
            Long count = jedis.sadd(key, values);
            if (time > 0) {
                expire(key, time);
            }
            return count;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        } finally {
            jedis.close();
        }
    }

    @Override
    public long getSetLength(String key) {
        Jedis jedis = pool.getResource();
        try {
            return jedis.scard(key);
        } finally {
            jedis.close();
        }
    }

    @Override
    public long removeSetby(String key, String... values) {
        Jedis jedis = pool.getResource();
        try {
            Long count = jedis.srem(key, values);
            return count;
        } finally {
            jedis.close();
        }
    }

    //===============================list==================================
    @Override
    public long getListSize(String key) {
        Jedis jedis = pool.getResource();
        try {
            return jedis.llen(key);
        } finally {
            jedis.close();
        }
    }

    @Override
    public Object getListIndex(String key, long index) {
        Jedis jedis = pool.getResource();
        try {
            return jedis.lindex(key, index);
        } finally {
            jedis.close();
        }
    }

    @Override
    public boolean addListL(String key, String... value) {
        Jedis jedis = pool.getResource();
        try {
            jedis.lpush(key, value);
            return true;
        } finally {
            jedis.close();
        }
    }

    @Override
    public boolean addListL(String key, String value, int time) {
        Jedis jedis = pool.getResource();
        try {
            addListL(key, value);
            if (time > 0) {
                expire(key, time);
            }
            return true;
        } finally {
            jedis.close();
        }
    }

    @Override
    public boolean addListR(String key, String... value) {
        Jedis jedis = pool.getResource();
        try {
            jedis.rpush(key, value);
            return true;
        } finally {
            jedis.close();
        }
    }

    @Override
    public boolean addListR(String key, int time, String... value) {
        Jedis jedis = pool.getResource();
        try {
            addListR(key, value);
            if (time > 0) {
                expire(key, time);
            }
            return true;
        } finally {
            jedis.close();
        }
    }

    @Override
    public boolean setByIndex(String key, int index, String value) {
        Jedis jedis = pool.getResource();
        try {
            jedis.lset(key, index, value);
            return true;
        } finally {
            jedis.close();
        }
    }

    @Override
    public long lRemove(String key, long count, String value) {
        Jedis jedis = pool.getResource();
        try {
            Long remove = jedis.lrem(key, count, value);
            return remove;
        } finally {
            jedis.close();
        }
    }

    @Override
    public void flushDB() {
        Jedis jedis = pool.getResource();
        try {
            jedis.flushDB();
        } finally {
            jedis.close();
        }
    }

}
